package com.zhongxi.guoyuan.widget.screenadaptaion;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2018/1/31.
 */

public class LayoutUtils {

    private static LayoutUtils instance;

    public static LayoutUtils getInstance(Context context) {
        if (instance == null) {
            instance = new LayoutUtils(context);
        }
        return instance;
    }

    public static final int STANDARD_WIDTH = 750;
    public static final int STANDARD_HEIGHT = 1334 - 48;
    private static final String DIMEN_CLASS = "com.android.internal.R$dimen";
    private static final String mSystem_bar_height = "status_bar_height";

    //实际设备分辨率
    public float displayMetricsWidth;
    public float displayMetricsHeight;

    private LayoutUtils(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (displayMetricsWidth == 0.0F || displayMetricsHeight == 0.0F) {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            //获取状态栏的高度
            int systemBarHeight = getSystemBarHeight(context);
            //获取手机真是的宽高
            if (displayMetrics.widthPixels > displayMetrics.heightPixels) {  //横屏
                this.displayMetricsWidth = (float) displayMetrics.heightPixels;
                this.displayMetricsHeight = (float) (displayMetrics.widthPixels - systemBarHeight);
            } else {  //竖屏
                this.displayMetricsWidth = (float) displayMetrics.widthPixels;
                this.displayMetricsHeight = (float) (displayMetrics.heightPixels - systemBarHeight);
            }
        }
    }

    private int getSystemBarHeight(Context context) {

        return getValue(context, DIMEN_CLASS, mSystem_bar_height, 48);
    }

    /**
     * @param context
     * @param attrGroupClass 安卓源代码找到的存放围度的类   类名
     * @param attrName      方法名
     * @param defaultValue   默认值
     * @return
     */
    private int getValue(Context context, String attrGroupClass, String attrName, int defaultValue) {
        try {
            Class<?> aClass = Class.forName(attrGroupClass);
            Object o = aClass.newInstance();
            Field field = aClass.getField(attrName);
            int i = Integer.parseInt(field.get(o).toString());
            return context.getResources().getDimensionPixelOffset(i);   //转换成像素
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    //开始获取缩放以后的结果
    public float getWidth(float width) {
        return width * displayMetricsWidth / STANDARD_WIDTH;
    }

    public float getHeight(float height) {
        return height * displayMetricsHeight / STANDARD_HEIGHT;
    }

    public int getWidth(int width) {
        return (int) (width * displayMetricsWidth / STANDARD_WIDTH);
    }

    public int getHeight(int height) {
        return (int) (height * displayMetricsHeight / STANDARD_HEIGHT);
    }
}
